#include <stdio.h>
#include <iostream>
#include "K8055D_C.h"
#include "resource.h"
#include "conio.h"
#include <cstdlib>

using namespace std;

bool CheckInputs(bool screen = true);

bool exitprog = false;
bool DigitalOutputStatus[8];
bool inputflag = false;

int main()
{
	bool output_on = false;

	// open communication with K8055D
	OpenDevice(0);

	// check for sticky inputs
	CheckInputs();

	// clear DigitalOutputStatus array
	for (int i = 0; i < 8; i++)
		DigitalOutputStatus[i] = false;

	cout << "Inital Check Complete" << endl;
	_getch();
	system("CLS");	
	
	do
	{
		output_on = CheckInputs(false);

		if (ReadDigitalChannel(5))
		{
			while (ReadDigitalChannel(5));

			if (output_on == true)
				exitprog = false;
			else
				exitprog = true;

			ClearAllDigital();

			for (int i = 0; i < 8; i++)
				DigitalOutputStatus[i] = false;

			inputflag = false;
		}
	}
	while (exitprog == false); // loop while button 5 is not depressed
	
	ClearAllDigital();

	for (int i = 0; i < 8; i++)
		DigitalOutputStatus[i] = false;

	cout << "Press any key to continue...";
	_getch();
	
	CloseDevice();
	return 0;
}

bool CheckInputs(bool screen)
{
	for (int i = 1; i <= 5; i++)
	{
		bool inputstatus = false;

		inputstatus = ReadDigitalChannel(i);

		if (inputstatus)
		{
			if (screen)
			{
				cout << "Input " << i << " depressed" << endl;	
				inputflag = true;
			}
			else
			{
				if (i != 5)
					if (inputflag == false)
					{
						SetDigitalChannel(i);
						DigitalOutputStatus[i - 1] = true;
						inputflag = true;
					}
					else;
				else;
			}
			
		}
		else;
	}

	if (inputflag == false && screen == true)
	{
		cout << "No inputs depressed\n" << endl;
		ClearAllDigital();
		
		for (int i = 0; i < 8; i++)
			DigitalOutputStatus[i] = false;
	}
	else;

	return inputflag;
}